﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows;
using System.Windows.Media.Animation;

using System.Drawing;

namespace EzDesk.Generic.WPF
{
    public static class WindowExtesion
    {

        public static T ToBeTop<T>(this T source) where T : System.Windows.Window
        {
            if (source == null) return null;
            if (!source.Topmost)
                source.Topmost = true;
            source.Topmost = false;
            return source;
        }

        public static Window MoveToRightBottom(this System.Windows.Window source)
        {
            if (source == null) return null;
            Rectangle workArea = Screen.PrimaryScreen.WorkingArea;
            source.Top = workArea.Height - source.Height;
            source.Left = workArea.Width - source.Width;
            return source;
        }

        public static Window MoveToCenter(this Window source)
        {
            if (source == null) return null;
            Rectangle workArea = Screen.PrimaryScreen.WorkingArea;
            source.Top = 500;
            source.Left = 500;
            return source;
        }


        public static Window ToBesideAndAutoMove(this Window source, Window besideWindow, WindowPostionDirection direction = WindowPostionDirection.Left, double xOffset = 0, double yOffset = 0, double limiationDistance = 50)
        {
            if (source == null || besideWindow == null) return null;
            source.ToBeside(besideWindow, direction: direction, xOffset: xOffset, yOffset: yOffset);
            source.ToBeInScreenWith(besideWindow, limiationDistance: limiationDistance);
            return source;


        }

        public static Window ToBeInScreenWith(this Window source, Window besideWindow, WindowPostionDirection direction = WindowPostionDirection.Left, double limiationDistance = 50)
        {
            if (source == null || besideWindow == null) return null;

            if (source.Left <= limiationDistance)
            {
                source.Left = limiationDistance;
                besideWindow.Left += source.Left + source.Width;
            }

            return source;
        }

        public static Window ToBeside(this Window source, Window besideWindow, WindowPostionDirection direction = WindowPostionDirection.Left, double xOffset = 0, double yOffset = 0)
        {
            if (source == null || besideWindow == null) return null;

            switch (direction)
            {
                case WindowPostionDirection.Left:
                    source.Left = besideWindow.Left - source.Width;
                    source.Top = besideWindow.Top;
                    break;
                case WindowPostionDirection.Right:
                    source.Left = besideWindow.Left + besideWindow.Width;
                    source.Top = besideWindow.Top;
                    break;
                case WindowPostionDirection.Up:
                    source.Left = besideWindow.Left;
                    source.Top = besideWindow.Top - source.Height;
                    break;
                case WindowPostionDirection.Down:
                    source.Left = besideWindow.Left;
                    source.Top = besideWindow.Top + besideWindow.Height;
                    break;
            }
            source.Left += xOffset;
            source.Top += yOffset;
            return source;
        }

        public static bool IsDisplayOnScreen(this Window source)
        {
            if (!source.IsVisible || source.WindowState == WindowState.Minimized)
                return false;

            return true;
        }

        public static Window ToBesideScreen(this Window source, ContentAlignment contentAlignment = ContentAlignment.TopCenter,double xOffset=0,double yOffset=0)
        {
            Rectangle workArea = Screen.PrimaryScreen.WorkingArea;

            switch (contentAlignment)
            {
                case ContentAlignment.TopCenter:
                    source.Top = 0;
                    source.Left = (workArea.Width - source.Width) / 2 ;
                    break;
                case ContentAlignment.MiddleRight:
                    source.Top = (workArea.Height - source.Height) / 2;
                    source.Left = workArea.Width - source.Width;
                    break;
                case ContentAlignment.TopLeft:
                    source.Top = 0;
                    source.Left = 0;
                    break;
                case ContentAlignment.TopRight:
                    source.Top = 0;
                    source.Left = workArea.Width - source.Width;
                    break;
                case ContentAlignment.BottomRight:
                    source.Top = workArea.Height - source.Height;
                    source.Left = workArea.Width - source.Width;
                    break;

                default:
                    break;
                //    break;
                //case ContentAlignment.TopCenter:
                //    break;
                //case ContentAlignment.TopCenter:
                //    break;
                //case ContentAlignment.TopCenter:
                //    break;
                //case ContentAlignment.TopCenter:
                //    break;
                //case ContentAlignment.TopCenter:
                //    break;
                //case ContentAlignment.TopCenter:
                //    break;
            }

            source.Top += yOffset;
            source.Left += xOffset;
            return source;

        }
    }
}
